package it.antonio.ilpizze.te.token;

import java.util.Map;

import it.antonio.ilpizze.reflection.MethodAccessor;
import it.antonio.ilpizze.te.Filter;
import it.antonio.ilpizze.te.RenderCommand;
import it.antonio.ilpizze.te.util.ValueExtractor;

public class ValueRenderCommand implements RenderCommand {
	
	private String name;
	private String property;
	private String filterName;
	
	public ValueRenderCommand(String name, String property, String filterName) {
		this.name = name;
		this.property = property;
		this.filterName = filterName;
	}

	@Override
	public void contribute(Map<String, Object> model, Map<String, Filter> filters, StringBuffer buffer) {
		Object object = model.get(name);
		if(object != null){
			
			if(property != null){
				MethodAccessor method = ValueExtractor.getter(object.getClass(), property);
				object = method.invoke(object);	
			} 
			
			Filter filter = filters.get(filterName);
			if(filter != null){
				buffer.append(filter.filter(object));
			} else {
				buffer.append(object);
			}
		} else {
			buffer.append("null");
		}
	}
	
}
